package cn.ekuma.pos.pda;

import cn.ekuma.pos.android.pos.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android_serialport_api.SerialPortFinder;

public class PreferencesActivity extends PreferenceActivity
implements OnSharedPreferenceChangeListener{
	public static final String KEY_SERVICE_ADDR = "preferences_server_addr";
	public static final String KEY_SERVICE_ADDR_DEFAULT = "http://192.168.1.10:8080/e-pos";
	public static String KEY_LOCAL_MACHINE_NAME = "preferences_local_machine_name";
	
	private EditTextPreference service_Addr;
	public SerialPortFinder mSerialPortFinder = new SerialPortFinder();
	private UsbManager mUsbManager;

	 @Override
	  protected void onCreate(Bundle icicle) {
	    super.onCreate(icicle);
	    addPreferencesFromResource(R.xml.preferences);

	    PreferenceScreen preferences = getPreferenceScreen();
	    preferences.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

	    service_Addr=(EditTextPreference)preferences.findPreference(KEY_SERVICE_ADDR);
	    
	 // Devices display
	 	final ListPreference devices = (ListPreference)findPreference("DEVICE_DISPLAY");
	    String[] entries = mSerialPortFinder.getAllDevices();
	    String[] entryValues = mSerialPortFinder.getAllDevicesPath();
	 		devices.setEntries(entries);
	 		devices.setEntryValues(entryValues);
	 		devices.setSummary(devices.getValue());
	 		devices.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
	 			public boolean onPreferenceChange(Preference preference, Object newValue) {
	 				preference.setSummary((String)newValue);
	 				return true;
	 			}
	 		});

	 		// Baud rates
	 		final ListPreference baudrates = (ListPreference)findPreference("DEVICE_DISPLAY_BAUDRATE");
	 		baudrates.setSummary(baudrates.getValue());
	 		baudrates.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
	 			public boolean onPreferenceChange(Preference preference, Object newValue) {
	 				preference.setSummary((String)newValue);
	 				return true;
	 			}
	 		});
	 		
	 		mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
	 	// Devices printer
		 	final ListPreference devicesPrint = (ListPreference)findPreference("DEVICE_PRINTER");
		 	
		    String[] entriesw = new String[mUsbManager.getDeviceList().size()];
		    mUsbManager.getDeviceList().keySet().toArray(entriesw); 
		    devicesPrint.setEntries(entriesw);
		    devicesPrint.setEntryValues(entriesw);
		    devicesPrint.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
		 			public boolean onPreferenceChange(Preference preference, Object newValue) {
		 				preference.setSummary((String)newValue);
		 				return true;
		 			}
		 	});	
	 }
	

	  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
	    disableLastCheckedPref();
	  }

	  private void disableLastCheckedPref() {
	   
	  }
	
}
